/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.manage;

import org.zhiqim.account.dbo.user.UserTesting;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsIntegerValue;
import org.zhiqim.httpd.validate.ones.IsLen;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Updater;

/**
 * 用户测试配置
 *
 * @version v1.4.1 @author zouzhigang 2018-4-21 新建与整理
 */
public class UserTestingAction extends StdSwitchAction
{
    @Override
    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("userId", "请选择一个选项"));
    }

    @Override
    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("userId", "用户编号不能为空"));
        request.addValidate(new IsIntegerValue("testingStatus", "测试状态必须是[0, 1]", 0, 1));
        request.addValidate(new IsLen("testingUrl", "测试地址不能为空且不超过128个字符", 1, 128));
    }

    @Override
    protected void list(HttpRequest request) throws Exception
    {
        int page = request.getParameterInt("page", 1);
        int pageSize = request.getContextAttributeInt("zmr_page_size", 20);
        
        PageResult<UserTesting> result = Global.get(ZTable.class).page(UserTesting.class, page, pageSize);
        result.addConditionMap(request.getParameterMap());
        
        request.setAttribute("result", result);
    }

    @Override
    protected void add(HttpRequest request) throws Exception
    {
    }

    @Override
    protected void modify(HttpRequest request) throws Exception
    {
        long userId = request.getParameterLong("userId");
        UserTesting item = Global.get(ZTable.class).item(UserTesting.class, userId);
        if (item == null)
        {
            request.returnHistory("该测试账号不存在");
            return;
        }
        
        request.setAttribute("item", item);
    }

    @Override
    protected void insert(HttpRequest request) throws Exception
    {
        UserTesting testing = new UserTesting();
        testing.setUserId(request.getParameterLong("userId"));
        testing.setTestingStatus(request.getParameterInt("testingStatus"));
        testing.setTestingUrl(request.getParameter("testingUrl"));
        
        Global.get(ZTable.class).insert(testing);
    }

    @Override
    protected void update(HttpRequest request) throws Exception
    {
        Updater updater = new Updater();
        updater.addMust("userId", request.getParameterLong("userId"));
        updater.addField("testingStatus", request.getParameterInt("testingStatus"));
        updater.addField("testingUrl", request.getParameter("testingUrl"));
        
        Global.get(ZTable.class).update(UserTesting.class, updater);
    }

    @Override
    protected void delete(HttpRequest request) throws Exception
    {
        long userId = request.getParameterLong("userId");
        if (Global.get(ZTable.class).count(UserTesting.class, userId) == 0)
        {
            request.returnHistory("该测试账号不存在");
            return;
        }
        
        Global.get(ZTable.class).delete(UserTesting.class, userId);
    }

}
